﻿using System;
using System.Runtime.CompilerServices;
using eLibrary.Environment;

namespace Letter.IO.Scheduler;

public static class SchedulerProvider
{
    static SchedulerProvider()
    {
        inline = new PipelineInlineScheduler();
        threadPool = new PipelineThreadPoolScheduler();
        allotter = SchedulerProvider.CreateAllotter();
    }

    private readonly static APipelineScheduler inline;
    private readonly static APipelineScheduler threadPool;
    private readonly static PipelineSchedulerAllotter allotter;

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static PipelineSchedulerAllotter GetSharedAllotter()
    {
        return allotter;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static APipelineScheduler GetInline()
    {
        return inline;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static APipelineScheduler GetThreadPool()
    {
        return threadPool;
    }

    public static APipelineScheduler CreateSingleScheduler()
    {
        return new PipelineSingleScheduler();
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static PipelineSchedulerAllotter CreateAllotter()
    {
        int processorCount = Environment.ProcessorCount;
        if (OS.IsWindows() || processorCount <= 32)
            processorCount = Math.Min(processorCount, 16);
        return new PipelineSchedulerAllotter(processorCount);
    }
}
